package com.eastmelon.blog.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eastmelon.blog.entity.Channel;
import com.eastmelon.blog.service.ChannelService;
import com.eastmelon.blog.service.IndexService;

/**
 * @ClassName: ChannelController
 * @Description: 栏目控制器
 * @author eastmelon
 */
@Controller("channelController")
@RequestMapping("/back/channel")
public class ChannelController {
	@Resource(name = "channelService")
	private ChannelService channelService;
	@Resource(name = "indexService")
	private IndexService indexService;
	
	@RequestMapping("/channelInfor")
	public String channelInfor(Model model){
		List<Channel> lc=channelService.findParentAndSon();
		model.addAttribute("lc", lc);
		return "back/channel/channelInfor";
	}

	@RequestMapping("/saveChannel")
	public String save(Channel channel) {
		Integer pos = null;
		// 设置位置
		if (channel.getPid() == 0) {
			// 获取最大位置
			pos = channelService.getMaxPPos();
		}
		else {
			pos = channelService.getMaxCPos();
		}
		if (pos == null) {
			channel.setPos(1);
		}
		else {
			channel.setPos(pos + 1);
		}
		channelService.save(channel);
		indexService.createHeader();
		indexService.createNav();
		return "redirect:/back/channel/channelInfor.do";
	}

	@RequestMapping("/deleteChannel")
	public String deleteById(int id) {
		channelService.deleteById(id);
		indexService.createHeader();
		indexService.createNav();
		return "redirect:/back/channel/channelInfor.do";
	}

	@RequestMapping("deleteChannels")
	public String deleteByIds(String ids) {
		String[] idsS=ids.split(",");
		channelService.deleteByIds(idsS);
		indexService.createHeader();
		indexService.createNav();
		return "redirect:/back/channel/channelInfor.do";
	}

	@RequestMapping("/updateChannel")
	public String updateChannel(Channel channel) {
		channelService.update(channel);
		indexService.createHeader();
		indexService.createNav();
		return "redirect:/back/channel/channelInfor.do";
	}
}
